package com.sea.physical.controller;


import com.sea.physical.entity.StockCreateRecord;
import com.sea.physical.entity.StockCreateSku;
import com.sea.physical.service.StockCreateSkuService;
import com.codecrane.core.util.DateUtils;
import com.codecrane.core.util.ExcelUtil;
import com.codecrane.core.web.Ajax;
import com.codecrane.core.web.AjaxReturn;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping(value = "/sea/physical/stockCreate")
public class StockCreateSkuController {

    @Autowired
    private StockCreateSkuService stockCreateSkuService;

    /**
     * 分页条件查询
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/findByCreateId")
    public List<StockCreateSku> findByCreateId(@RequestParam("createId") Integer createId) {
        List<StockCreateSku> list = stockCreateSkuService.findByCreateId(createId);
        return list;
    }

    @ResponseBody
    @RequestMapping("/cancel")
    public AjaxReturn cancel(@RequestParam("id") Integer id){
        AjaxReturn result = Ajax.fail().setMsg("删除失败！");
        try {
            stockCreateSkuService.cancel(id);
            result.setOk(true).setMsg("删除成功！");
        }catch (Exception e){
            e.printStackTrace();
            result.setMsg("系统异常,请联系管理员!");
        }
        return result;
    }

    /**
     * 保存
     *
     * @param createId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/send", method = RequestMethod.POST)
    public AjaxReturn send(StockCreateRecord stockCreateRecord) {
        AjaxReturn result = Ajax.fail().setMsg("发运失败！");
        try {
            stockCreateSkuService.send(stockCreateRecord);
            result.setOk(true).setMsg("发运成功！");
        }catch (RuntimeException e){
            result.setMsg(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            result.setMsg("系统异常,请联系管理员!");
        }
        return result;
    }

    /**
     * 导入Code
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/importSku", method = RequestMethod.POST)
    public AjaxReturn importDetail(MultipartFile file, @RequestParam("createId")Integer createId) {
        AjaxReturn result = Ajax.fail().setMsg("保存失败！");
        try {
            if (null == file || file.isEmpty()) {
                return result.setMsg("请上传文件！！！");
            } else {
                String fileName = file.getOriginalFilename();
                if (!(fileName.contains(".xls"))) {
                    return result.setMsg("只能上传excel文件！！！");
                }
                InputStream is = file.getInputStream();
                stockCreateSkuService.importSku(is,fileName,createId);
                result.setOk(true);
                result.setMsg("上传成功！！");
            }
        }catch (RuntimeException e){
            e.printStackTrace();
            result = Ajax.fail().setMsg(e.getMessage());
        }
        catch (Exception e){
            e.printStackTrace();
            result = Ajax.fail().setMsg("系统异常请联系管理员！");
        }
        return result;
    }


    /**
     * 导出当前数据
     *
     * @param request
     * @param response
     */
    @RequestMapping(value = "/export")
    @ResponseBody
    public void export(HttpServletRequest request, HttpServletResponse response,StockCreateSku stockCreateSku ) {
        List<StockCreateSku> receiveList = stockCreateSkuService.export(stockCreateSku);

        String[] title = {"SKU","柜号","数量","产品名称","创建时间","发运时间","送货时间","入库时间"};
        String fileName = "海外仓" + DateUtils.dateToString(new Date(),"yyyyMMdd") + ".xls";
        String sheetName = "海外仓";
        String[][] content = new String[receiveList.size()][7];
        for (int i = 0; i < receiveList.size(); i++) {
            content[i] = new String[title.length];
            StockCreateSku obj = receiveList.get(i);
            content[i][0] = obj.getCreateSku();
            content[i][1] = obj.getCreateNo();
            content[i][2] = obj.getCreateNumber()+"";
            content[i][3] = obj.getProductName();
            content[i][4] = obj.getCreateTime();
            content[i][5] = obj.getSendTime()+"";
            content[i][6] = obj.getArriveTime()+"";
            content[i][7] = obj.getInTime()+"";
        }
        //创建HSSFWorkbook
        HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, content, null);
        //响应到客户端
        try {
            this.setResponseHeader(response, fileName);
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //发送响应流方法
    public void setResponseHeader(HttpServletResponse response, String fileName) {
        try {
            try {
                fileName = new String(fileName.getBytes(),"ISO8859-1");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            response.setContentType("application/octet-stream;charset=ISO8859-1");
            response.setHeader("Content-Disposition", "attachment;filename="+ fileName);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
